//
//  BuyOrSellViewModel.swift
//  Exchange
//
//  Created by rui on 2018/4/23.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

enum BuyOrSellType: Int {
    case buy = 1
    case sell = 0
}

class AdvertisingListViewModel: BaseViewModel {
    
    var isNoMoreData = false
    
    var type: BuyOrSellType = .buy
    var page: Int = 1
    let size: Int = 15
    ///币种
    var moneyType: String = ""
    

    
    var dataArray = [AdvertisingListModel]()
    
    private lazy var advertisingApi: AdvertisingListRequest = {
        let api = AdvertisingListRequest()
        api.dataReformer = self
        api.paramSource = self
        api.delegate = self
        return api
    }()
    
    
    func addAdvertisingListBaseParam(moneyType: String,type: BuyOrSellType) {
        self.moneyType = moneyType
        self.type = type
    }
    
    func loadNewData() {
        self.page = 1
        self.advertisingApi.loadData()
    }
    

    
    func loadMoreData() {
        self.page += 1
        self.advertisingApi.loadData()
    }
}

extension AdvertisingListViewModel: RequestCallBack,RequestDataReformer,RequestParamSource {
    
    func reform(_ request: BaseRequest, _ data: [String : Any]?) {
        if let _ = request as? AdvertisingListRequest {
            var flag = false
            if let realyData = data?["data"] as? [String:Any] {
                if let list = realyData["list"] as? [[String:Any]] {
                    if let jsonData = list.jsonData {
                        do{
                            let list = try JSONDecoder().decode(Array<AdvertisingListModel>.self, from: jsonData)
                            if self.page == 1 {
                                self.dataArray.removeAll()
                            }
                            if let hasNext =  realyData["hasNextPage"] as? Bool {
                                self.isNoMoreData = !hasNext
                            }
                            self.dataArray.append(contentsOf: list)
                            flag = true
                        }catch {
                            flag = false
                            dPrint(error)
                        }
                    }
                }
            }
            if !flag {
                self.page -= 1
            }
            self.response?(ResponseState.trade_ad_list,flag, nil)
        }
    }
    
    func requestDidSucess(_ request: BaseRequest) {
        if let _ = request as? AdvertisingListRequest {
            self.isNoMoreData = false
        }
    }
    
    func requestDidFail(_ request: BaseRequest) {
        if let _ = request as? AdvertisingListRequest {
            self.isNoMoreData = false
            self.page -= 1
            self.response?(ResponseState.trade_ad_list,false, request.errorMsg)
        }
    }
    
    func requestParams(_ request: BaseRequest) -> [String : Any]? {
        var pramas: [String:Any] = [:]
        if let _ = request as? AdvertisingListRequest {
            pramas.updateValue(moneyType, forKey: "moneyType")
            pramas.updateValue(size, forKey: "size")
            pramas.updateValue(page, forKey: "page")
            pramas.updateValue(type.rawValue, forKey: "type")
        }
        return pramas
    }
    
}
